package io.renren.sf.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.*;
import io.renren.sf.entity.*;
import io.renren.sf.service.CbStiffChapterService;
import io.renren.sf.service.CbStiffSystemService;
import io.renren.sf.service.CbStiffUnitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.service.CbStiffBookService;


/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-26 10:46:38
 */
@RestController
@RequestMapping("sf/cbstiffbook")
public class CbStiffBookController {
    @Autowired
    private CbStiffBookService cbStiffBookService;

    @Autowired
    private CbStiffSystemService cbStiffSystemService;

    /**
     * 发布资源
     * @param bid
     * @return
     */
    @GetMapping("/createZip/{bid}")
    public R createZip(@PathVariable("bid") long bid){
        try {
            //创建文件夹
            String zipFile = cbStiffBookService.createZip(bid);
            if(!zipFile.contains(String.valueOf(bid))){
                return R.error(zipFile);
            }
            CbStiffBookEntity byId = cbStiffBookService.getById(bid);
            byId.setZipFile(zipFile);
            cbStiffBookService.updateById(byId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("资源包不完整，还需要添加数据");
        } finally {
            //关闭文件流
            System.gc();
            System.runFinalization(); // 强制回收io流
            File zip = new File("E:\\" + bid + ".zip");
//            File zip = new File("/tmp/" + bid + ".zip");
            if(zip.exists()){
                // 删除压缩包
                zip.delete();
            }
            File dir = new File(Constant.Zip.PATH.getName());
            if (dir.exists()) {
                if (dir.isDirectory()) {
                    String[] children = dir.list();
                    //递归删除目录中的子目录下
                    for (int i = 0; i < children.length; i++) {
                        (new File(dir, children[i])).delete();
                    }
                }
                dir.delete();
            }
        }
    }
    /**
     * 获取硬笔系统list
     */
    @GetMapping("/getSystemList")
    public R getSystemList() {
        List<CbStiffSystemEntity> systemList = cbStiffSystemService.list();
        return R.ok().put("systemList", systemList);
    }

    /**
     * 硬笔书分页systemId
     */
    @GetMapping("/list")
    public R bookEditionPage(@RequestParam Map<String, Object> params) {
        Page<CbStiffBookEntity> stiffBookPage = cbStiffBookService.getBookBySystemId(params);
        return R.ok().put("stiffBookPage", stiffBookPage);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbstiffbook:info")
    public R info(@PathVariable("id") Long id){
		CbStiffBookEntity cbStiffBook = cbStiffBookService.getById(id);

        return R.ok().put("cbStiffBook", cbStiffBook);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbstiffbook:save")
    public R save(@RequestBody CbStiffBookEntity cbStiffBook){
        cbStiffBook.setCreatetime(new Date());
		cbStiffBookService.save(cbStiffBook);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbstiffbook:update")
    public R update(@RequestBody CbStiffBookEntity cbStiffBook){
		cbStiffBookService.updateById(cbStiffBook);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbstiffbook:delete")
    public R delete(@RequestBody Long[] ids){
		cbStiffBookService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
